package nanocast.apps.creator.modules.sceneListModule.model {
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	
	import org.puremvc.as3.multicore.patterns.proxy.Proxy;
	
	import nanocast.apps.creator.modules.sceneListModule.ApplicationFacade;	

	public class SceneListProxy extends Proxy  
	{	
		
		public static const NAME:String = "SceneList";
		
		private var loader : URLLoader;
		private var sceneXml : XML;
		private var assetPath:String = "assets/scene.xml";
		
		public function SceneListProxy()
		{	
			super(NAME);
			loadSceneXml();
		}
		
		private function loadSceneXml():void
		{
			var urlReq : URLRequest = new URLRequest(assetPath);
			loader = new URLLoader();
			loader.addEventListener(Event.COMPLETE, loadCompleteListener);
			loader.addEventListener(IOErrorEvent.IO_ERROR, ioErrorListener);
			loader.load(urlReq);
		}
		
		private function loadCompleteListener(e : Event) : void 
		{
			sceneXml = XML(loader.data);
			sendNotification(ApplicationFacade.LOADING_COMPLETE, sceneXml);
		}
		
		private function ioErrorListener(e : IOErrorEvent):void
		{
			throw new Error("Error while loading "+assetPath);
		}
		
		public function getSceneXml():XML
		{
			return sceneXml;
		}
	}
}